home *** CD-ROM | disk | FTP | other *** search
/ Collection of Internet / Collection of Internet.iso / msdos / lynx / source / www / library / implemen / htlex.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-25  |  3.1 KB  |  142 lines

  1.  
  2. /* MODULE                            HTLex.c
  3. **        LEXICAL ANALYSOR
  4. **
  5. ** AUTHORS:
  6. **    AL    Ari Luotonen    luotonen@dxcern.cern.ch
  7. **
  8. ** HISTORY:
  9. **
  10. **
  11. ** BUGS:
  12. **
  13. **
  14. */
  15. #include"capalloc.h"
  16. #include"capstdio.h"
  17. #include "HTAAUtil.h"
  18. #include "HTLex.h"    /* Implemented here */
  19.  
  20.  
  21. /*
  22. ** Global variables
  23. */
  24. PUBLIC char lex_buffer[40];    /* Read lexical string        */
  25. PUBLIC int lex_line = 1;    /* Line number in source file    */
  26.  
  27.  
  28. /*
  29. ** Module-wide variables
  30. */
  31. PRIVATE int lex_cnt;
  32. PRIVATE BOOL lex_template;
  33. PRIVATE LexItem lex_pushed_back = LEX_NONE;
  34. PRIVATE FILE *cache = NULL;
  35.  
  36.  
  37. PUBLIC void unlex ARGS1(LexItem, lex_item)
  38. {
  39.     lex_pushed_back = lex_item;
  40. }
  41.  
  42.  
  43. PUBLIC LexItem lex ARGS1(FILE *, fp)
  44. {
  45.     int ch;
  46.  
  47.     if (fp != cache) {    /* This cache doesn't work ok because the system  */
  48.     cache = fp;    /* often assign same FILE structure the next open */
  49.     lex_line = 1;    /* file. So, if there are syntax errors in setup  */
  50.     }            /* files it may confuse things later on.      */
  51.  
  52.     if (lex_pushed_back != LEX_NONE) {
  53.     LexItem ret = lex_pushed_back;
  54.     lex_pushed_back = LEX_NONE;
  55.     return ret;
  56.     }
  57.  
  58.     lex_cnt = 0;
  59.     lex_template = NO;
  60.  
  61.     for(;;) {
  62.     switch (ch = getc(fp)) {
  63.       case EOF:
  64.       case ' ':
  65.       case '\t':
  66.       case '\r':
  67.       case '\n':
  68.       case ':':
  69.       case ',':
  70.       case '(':
  71.       case ')':
  72.       case '@':
  73.         if (lex_cnt > 0) {
  74.         if (ch != EOF) ungetc(ch,fp);
  75.         if (lex_template) return LEX_TMPL_STR;
  76.         else          return LEX_ALPH_STR;
  77.         }
  78.         else switch(ch) {
  79.           case EOF:        return LEX_EOF;        break;
  80.           case '\n':
  81.         lex_line++;    return LEX_REC_SEP;    break;
  82.           case ':':        return LEX_FIELD_SEP;    break;
  83.           case ',':        return LEX_ITEM_SEP;    break;
  84.           case '(':        return LEX_OPEN_PAREN;    break;
  85.           case ')':        return LEX_CLOSE_PAREN;    break;
  86.           case '@':        return LEX_AT_SIGN;    break;
  87.           default:    ;    /* Leading white space ignored (SP,TAB,CR) */
  88.         }
  89.         break;
  90.       default:
  91.         lex_buffer[lex_cnt++] = ch;
  92.         lex_buffer[lex_cnt] = (char)0;
  93.         if ('*' == ch) lex_template = YES;
  94.     } /* switch ch */
  95.     } /* forever */
  96. }
  97.  
  98.  
  99. PUBLIC char *lex_verbose ARGS1(LexItem, lex_item)
  100. {
  101.     static char msg[100];
  102.  
  103.     switch (lex_item) {
  104.       case LEX_NONE:        /* Internally used    */
  105.     return "NO-LEX-ITEM";
  106.     break;
  107.       case LEX_EOF:        /* End of file        */
  108.     return "end-of-file";
  109.     break;
  110.       case LEX_REC_SEP:        /* Record separator    */
  111.     return "record separator (newline)";
  112.     break;
  113.       case LEX_FIELD_SEP:    /* Field separator    */
  114.     return "field separator ':'";
  115.     break;
  116.       case LEX_ITEM_SEP:    /* List item separator    */
  117.     return "item separator ','";
  118.     break;
  119.       case LEX_OPEN_PAREN:    /* Group start tag    */
  120.     return "'('";
  121.     break;
  122.       case LEX_CLOSE_PAREN:    /* Group end tag    */
  123.     return "')'";
  124.     break;
  125.       case LEX_AT_SIGN:        /* Address qualifier    */
  126.     return "address qualifier '@'";
  127.     break;
  128.       case LEX_ALPH_STR:    /* Alphanumeric string    */
  129.     sprintf(msg, "alphanumeric string '%s'", lex_buffer);
  130.     return msg;
  131.     break;
  132.       case LEX_TMPL_STR:    /* Template string    */
  133.     sprintf(msg, "template string '%s'", lex_buffer);
  134.     return msg;
  135.     break;
  136.       default:
  137.     return "UNKNOWN-LEX-ITEM";
  138.     break;
  139.     }
  140. }
  141.  
  142.